package com.css.dzj.dzjjcss.modules.todo.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.BeanUtil;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.todo.entity.JcssTodoEntity;
import com.css.dzj.dzjjcss.modules.todo.service.TodoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/todo")
public class TodoController {
    @Autowired
    private TodoService todoService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 新增待办事项
     *
     * @param jcssTodoEntity
     * @return
     */
    @GetMapping("/add")
    public R add(@RequestBody JcssTodoEntity jcssTodoEntity) {
        String s="";
        try {
            todoService.add(jcssTodoEntity);
            s = JSON.toJSONString(jcssTodoEntity);
            sysAuditService.saveSuccessAudit("待办添加成功", Constant.DBYB, Constant.PT,Constant.ADD,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办添加失败",Constant.DBYB,Constant.PT,Constant.ADD,s,e,request);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 逻辑删除待办事项
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R delete(String id) {
        String s="";
        try {
            todoService.delete(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("待办删除成功", Constant.DBYB, Constant.PT,Constant.DEL,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办删除失败",Constant.DBYB,Constant.PT,Constant.DEL,s,e,request);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 修改待办事项
     *
     * @param jcssTodoEntity
     * @return
     */
    @GetMapping("/update")
    public R update(@RequestBody JcssTodoEntity jcssTodoEntity) {
        String s="";
        try {
            JcssTodoEntity oldjcssTodoEntity = todoService.selectOne(jcssTodoEntity.getId());
            oldjcssTodoEntity.setUpdateTime(new Date());
            BeanUtil.copyBeanNotEmptyToBean(jcssTodoEntity, oldjcssTodoEntity);
            todoService.update(jcssTodoEntity);
            s = JSON.toJSONString(jcssTodoEntity);
            sysAuditService.saveSuccessAudit("待办修改成功", Constant.DBYB, Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办修改失败",Constant.DBYB,Constant.PT,Constant.UPDATE,s,e,request);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 查询单个待办事项
     *
     * @param id
     * @return
     */
    @GetMapping("/selectOne")
    public R selectOne(@RequestBody String id) {
        String s="";
        try {
            JcssTodoEntity jcssTodoEntity = todoService.selectOne(id);
            s = JSON.toJSONString(jcssTodoEntity);
            sysAuditService.saveSuccessAudit("待办查询成功", Constant.DBYB, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", jcssTodoEntity);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办查询失败",Constant.DBYB,Constant.PT,Constant.SELETE,s,e,request);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 无条件查询所有待办事项
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/selectAll")
    public R allList(@RequestParam(name = "title", required = false) String title,
                     @RequestParam(name = "pageSize", required = false) Integer pageSize,
                     @RequestParam(name = "pageNo", required = false) Integer pageNo) {
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JcssTodoEntity> jlist = todoService.selectAll(pageRequest, title);
            s = JSON.toJSONString(jlist);
            sysAuditService.saveSuccessAudit("待办查询所有成功", Constant.DBYB, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", jlist);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办查询所有失败",Constant.DBYB,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询所属机构的代办事项
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/queryAll")
    public R allList(@RequestParam(name = "pageSize", required = false) Integer pageSize,
                     @RequestParam(name = "pageNo", required = false) Integer pageNo,
                     @RequestParam(name = "orgName", required = false) String orgName) {
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JcssTodoEntity> qlist = todoService.queryAll(pageRequest, orgName);
            s = JSON.toJSONString(qlist);
            sysAuditService.saveSuccessAudit("待办查询成功", Constant.DBYB, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", qlist);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("待办查询失败",Constant.DBYB,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

}
